package com.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Perpustakaan {

    public static String NAMA = "PERPUSTAKAAN UNIVERSITAS SANATA DHARMA";
    public static ArrayList<Publikasi> DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();
    public static ArrayList<Pinjaman> DAFTAR_PINJAMAN = new ArrayList<Pinjaman>();
    private TimerBacaPublikasi TIMER_BACA_PUBLIKASI;
    private TimerSimpanPublikasi TIMER_SIMPAN_PUBLIKASI;

    public static void tambahPublikasi(Publikasi publikasi) {
        DAFTAR_PUBLIKASI.add(publikasi);
    }

    public static void tambahAnggota(Anggota anggota) {
        DAFTAR_ANGGOTA.add(anggota);
    }

    public static void tambahPinjaman(Pinjaman pinjaman) {
        DAFTAR_PINJAMAN.add(pinjaman);
    }

    public static String bacaDaftarPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                hasil += "\nJudul      : " + DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
                hasil += " Id koleksi : " + DAFTAR_PUBLIKASI.get(i).getIdKoleksi() + "\n";
                hasil += " Penulis    : " + DAFTAR_PUBLIKASI.get(i).getPenulis() + "\n";
                hasil += " Penerbit   : " + DAFTAR_PUBLIKASI.get(i).getPenerbit() + "\n";
                hasil += " Tahun      : " + DAFTAR_PUBLIKASI.get(i).getTahun();
            }
            return hasil;
        } else {
            return "kosong";
        }

    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                hasil += "\nNama       : " + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
                hasil += " Id Anggota : " + DAFTAR_ANGGOTA.get(i).getIdAnggota();
            }
            return hasil;
        }
        return null;
    }

    public static String bacaDaftarPinjaman() {
        if (DAFTAR_PINJAMAN.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_PINJAMAN.size(); i++) {
                hasil += " Id Anggota     : " + DAFTAR_PINJAMAN.get(i).getPeminjam().getIdAnggota()
                        + "\n";
                hasil += " Id Koleksi      : " + DAFTAR_PINJAMAN.get(i).getPublikasi().getIdKoleksi()
                        + "\n";
            }
            return hasil;
        }
        return null;
    }

    public static void simpanFilePublikasi() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {

            try {
                File outFile = new File("publikasi.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void simpanFileAnggota() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {

            try {
                File outFile = new File("anggota.dat");
                outStream = new FileOutputStream(outFile);
                String data = new String();
                for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                    data += "Id Anggota : " + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "\tNama : "
                            + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
                }
                outStream.write(data.getBytes());
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void simpanFilePinjaman() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_PINJAMAN.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Pinjaman Kosong");
        } else {

            try {
                File outFile = new File("pinjaman.dat");
                outStream = new FileOutputStream(outFile);
                String data = new String();
                for (int i = 0; i < DAFTAR_PINJAMAN.size(); i++) {
                    data += "Id Anggota : " + DAFTAR_PINJAMAN.get(i).getPeminjam().getIdAnggota()
                            + "\tId Koleksi : "
                            + DAFTAR_PINJAMAN.get(i).getPublikasi().getIdKoleksi() + "\n";
                }
                outStream.write(data.getBytes());
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFilePublikasi() {
        try {
            FileInputStream inStream = null;
            File inFile = new File("publikasi.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = null;
            try {
                inObjectStream = new ObjectInputStream(inStream);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                DAFTAR_PUBLIKASI = (ArrayList<Publikasi>) inObjectStream.readObject();
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaFileAnggota() {
        try {
            FileInputStream inStream = null;
            String data = new String();
            File inFile = new File("anggota.dat");
            try {
                inStream = new FileInputStream(inFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            int a;
            while ((a = inStream.read()) != -1) {
                data = data + (char) a;
            }
            inStream.close();
            System.out.println(data);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaFilePinjaman() {
        try {
            FileInputStream inStream = null;
            String data = new String();
            File inFile = new File("pinjaman.dat");
            try {
                inStream = new FileInputStream(inFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            int a;
            while ((a = inStream.read()) != -1) {
                data = data + (char) a;
            }
            inStream.close();
            System.out.println(data);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void tambahTabelPublikasi(Publikasi publikasi) {
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
            Statement statement = conn.createStatement();
            String sql = "insert into publikasi (id_koleksi, judul) values ('"
                    + publikasi.getIdKoleksi() + "','" + publikasi.getJudul() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void tambahTabelAnggota(Anggota anggota) {
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
            Statement statement = conn.createStatement();
            String sql1 = "insert into anggota (ID_ANGGOTA, NAMA) values ('"
                    + anggota.getIdAnggota() + "','" + anggota.getNama() + "')";
            statement.executeUpdate(sql1);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void tambahTabelPinjaman(Pinjaman pinjaman) {
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
            Statement statement = conn.createStatement();
            String sql2 = "insert into pinjaman (ID_ANGGOTA, ID_KOLEKSI, TGL_PINJAM, TGL_KEMBALI) values ('"
                    + pinjaman.getPeminjam() + "','" + pinjaman.getPublikasi()
                    + "','" + pinjaman.getTanggalPinjam() + "','" + pinjaman.getTanggalKembali() + "')";
            statement.executeUpdate(sql2);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void simpanTabelPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
            Statement statement = null;
            try {
                statement = conn.createStatement();
                for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                    String sql = "insert into publikasi (id_koleksi, judul) values ('"
                            + DAFTAR_PUBLIKASI.get(i).getIdKoleksi() + "','"
                            + DAFTAR_PUBLIKASI.get(i).getJudul() + "')";
                    statement.executeUpdate(sql);
                }
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void simpanTabelAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
            Statement statement = null;
            try {
                statement = conn.createStatement();
                for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                    String sql1 = "insert into anggota (ID_ANGGOTA, NAMA) values ('"
                            + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "','"
                            + DAFTAR_ANGGOTA.get(i).getNama() + "')";
                    statement.executeUpdate(sql1);
                }
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void simpanTabelPinjaman() {
        if (DAFTAR_PINJAMAN.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Pinjaman Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
            Statement statement = null;
            try {
                statement = conn.createStatement();
                for (int i = 0; i < DAFTAR_PINJAMAN.size(); i++) {
                    String sql2 = "insert into pinjaman (ID_ANGGOTA, ID_KOLEKSI, TGL_PINJAM, TGL_KEMBALI) values ('"
                            + DAFTAR_PINJAMAN.get(i).getPeminjam() + "','"
                            + DAFTAR_PINJAMAN.get(i).getPublikasi() + "','"
                            + DAFTAR_PINJAMAN.get(i).getTanggalPinjam() + "','"
                            + DAFTAR_PINJAMAN.get(i).getTanggalKembali() + "')";
                    statement.executeUpdate(sql2);
                }
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaTabelPublikasi() throws SQLException {
        DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
        java.sql.Statement statement = conn.createStatement();
        String sql = "select * from publikasi";
        ResultSet result = statement.executeQuery(sql);

        while (result.next()) {
            Publikasi temp = new Publikasi() {

                public int hitungDenda(int lamaPinjam) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                @Override
                public int HitungDenda(int lamaPinjam) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
            temp.setIdKoleksi(result.getString("id_koleksi"));
            temp.setJudul(result.getString("judul"));
            temp.setPenerbit(result.getString("PENULIS"));
            temp.setPenerbit(result.getString("PENERBIT"));
            temp.setTahun(result.getString("TAHUN"));
            DAFTAR_PUBLIKASI.add(temp);
        }
        conn.close();
    }

    public static void bacaTabelAnggota() throws SQLException {
        DAFTAR_ANGGOTA = new ArrayList<Anggota>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "winda");
        java.sql.Statement statement = conn.createStatement();
        String sql1 = "select * from anggota";
        ResultSet result = statement.executeQuery(sql1);

        while (result.next()) {
            Anggota temp = new Anggota();
            temp.setNama(result.getString("NAMA"));
            temp.setIdAnggota(result.getString("ID_ANGGOTA"));
            DAFTAR_ANGGOTA.add(temp);
        }
        conn.close();
    }

    public static ArrayList<Anggota> cariAnggota(String nama) {
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
            Anggota temp = DAFTAR_ANGGOTA.get(i);

            if (temp.getNama().contains(nama)) {
                hasil.add(temp);
            } else {
                return null;
            }
        }
        return hasil;
    }

    public void aturTimer() {
    }
}
